// Kony

#pragma once

#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "LevelConfig.generated.h"

/**
 * 等级配置信息结构体,用于 创建升级经验需求和灵气DataTable
 */
USTRUCT(BlueprintType)
struct FLevelConfigInfo : public FTableRowBase
{
	GENERATED_BODY()

	// 修为境界名称
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	FString CultivationName;

	// 等级范围
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 MinLevel = 1;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 MaxLevel = 1;

	// 基础经验值
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 BaseXP = 100;

	// 经验值倍数
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float XPMultiplier = 1.0f;

	// 每级属性点奖励
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 AttributePointsPerLevel = 1;

	// 每级法术点奖励
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 SpellPointsPerLevel = 1;

	// 该阶段最大灵气值
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 MaxLingQi = 1000;
};

/**
 * 等级配置工具类，用于从DataTable计算经验值和等级
 */
UCLASS(BlueprintType)
class GASC_HUANLING_API ULevelConfig : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	// 计算指定等级所需的总经验值
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 CalculateXPForLevel(UDataTable* DataTable, int32 Level);

	// 根据当前等级和经验值查找对应等级
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 FindLevelForXP(UDataTable* DataTable, int32 CurrentLevel, int32 CurrentXP);

	// 根据当前等级和经验值判断升级后的等级
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 FindLevelForLevelAndXP(UDataTable* DataTable, int32 CurrentLevel, int32 CurrentXP);

	// 获取指定等级的配置信息
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static FLevelConfigInfo GetConfigForLevel(UDataTable* DataTable, int32 Level);

	// 判断是否可以升级
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static bool CanLevelUp(UDataTable* DataTable, int32 CurrentLevel, int32 CurrentXP);

	// 获取升级到下一级所需的经验值
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 GetXPRequiredForNextLevel(UDataTable* DataTable, int32 CurrentLevel, int32 CurrentXP);

	// 获取指定等级的最大灵气值
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 GetMaxLingQiForLevel(UDataTable* DataTable, int32 Level);

	// 计算灵气值百分比
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static float CalculateLingQiPercent(UDataTable* DataTable, int32 CurrentLevel, int32 CurrentLingQi);

	// 获取指定等级的属性点奖励
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 GetAttributePointsForLevel(UDataTable* DataTable, int32 Level);

	// 获取指定等级的技能点奖励
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 GetSpellPointsForLevel(UDataTable* DataTable, int32 Level);

	// 获取当前等级升级所需的经验值（不累计，只返回当前等级升级需要的经验）
	UFUNCTION(BlueprintCallable, Category = "Level Config")
	static int32 GetXPRequiredForCurrentLevel(UDataTable* DataTable, int32 Level);

private:
	// 从DataTable加载配置数据
	static TArray<FLevelConfigInfo> LoadConfigsFromDataTable(UDataTable* DataTable);

	// 查找等级对应的配置索引
	static int32 FindConfigIndexForLevel(const TArray<FLevelConfigInfo>& Configs, int32 Level);
};